﻿// Decompiled with JetBrains decompiler
// Type: UnityEngine.Experimental.GlobalIllumination.LightmapperUtils
// Assembly: UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E6AE6B5A-2752-4B07-9528-C8B77C62CE79
// Assembly location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll
// XML documentation location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.xml

using System;
using UnityEngine.Rendering;

#nullable disable
namespace UnityEngine.Experimental.GlobalIllumination
{
  /// <summary>
  ///   <para>Utility class for converting Unity Lights to light types recognized by the baking backends.</para>
  /// </summary>
  public static class LightmapperUtils
  {
    /// <summary>
    ///   <para>Extracts informations from Lights.</para>
    /// </summary>
    /// <param name="baketype">The lights baketype.</param>
    /// <returns>
    ///   <para>Returns the light's light mode.</para>
    /// </returns>
    public static LightMode Extract(LightmapBakeType baketype)
    {
      int num;
      switch (baketype)
      {
        case LightmapBakeType.Mixed:
          num = 1;
          break;
        case LightmapBakeType.Realtime:
          num = 0;
          break;
        default:
          num = 2;
          break;
      }
      return (LightMode) num;
    }

    /// <summary>
    ///   <para>Extracts the indirect color from a light.</para>
    /// </summary>
    /// <param name="l"></param>
    public static LinearColor ExtractIndirect(Light l)
    {
      return LinearColor.Convert(l.color, l.intensity * l.bounceIntensity);
    }

    /// <summary>
    ///   <para>Extracts the inner cone angle of spot lights.</para>
    /// </summary>
    /// <param name="l"></param>
    public static float ExtractInnerCone(Light l)
    {
      return 2f * Mathf.Atan((float) ((double) Mathf.Tan((float) ((double) l.spotAngle * 0.5 * (Math.PI / 180.0))) * 46.0 / 64.0));
    }

    private static Color ExtractColorTemperature(Light l)
    {
      Color colorTemperature = new Color(1f, 1f, 1f);
      if (l.useColorTemperature && GraphicsSettings.lightsUseLinearIntensity)
        colorTemperature = Mathf.CorrelatedColorTemperatureToRGB(l.colorTemperature);
      return colorTemperature;
    }

    private static void ApplyColorTemperature(Color cct, ref LinearColor lightColor)
    {
      lightColor.red *= cct.r;
      lightColor.green *= cct.g;
      lightColor.blue *= cct.b;
    }

    public static void Extract(Light l, ref DirectionalLight dir)
    {
      dir.instanceID = l.GetInstanceID();
      dir.mode = LightmapperUtils.Extract(l.lightmapBakeType);
      dir.shadow = l.shadows != 0;
      dir.position = l.transform.position;
      dir.orientation = l.transform.rotation;
      Color colorTemperature = LightmapperUtils.ExtractColorTemperature(l);
      LinearColor lightColor = LinearColor.Convert(l.color, l.intensity);
      LinearColor indirect = LightmapperUtils.ExtractIndirect(l);
      LightmapperUtils.ApplyColorTemperature(colorTemperature, ref lightColor);
      LightmapperUtils.ApplyColorTemperature(colorTemperature, ref indirect);
      dir.color = lightColor;
      dir.indirectColor = indirect;
      dir.penumbraWidthRadian = l.shadows == LightShadows.Soft ? (float) Math.PI / 180f * l.shadowAngle : 0.0f;
    }

    public static void Extract(Light l, ref PointLight point)
    {
      point.instanceID = l.GetInstanceID();
      point.mode = LightmapperUtils.Extract(l.lightmapBakeType);
      point.shadow = l.shadows != 0;
      point.position = l.transform.position;
      point.orientation = l.transform.rotation;
      Color colorTemperature = LightmapperUtils.ExtractColorTemperature(l);
      LinearColor lightColor = LinearColor.Convert(l.color, l.intensity);
      LinearColor indirect = LightmapperUtils.ExtractIndirect(l);
      LightmapperUtils.ApplyColorTemperature(colorTemperature, ref lightColor);
      LightmapperUtils.ApplyColorTemperature(colorTemperature, ref indirect);
      point.color = lightColor;
      point.indirectColor = indirect;
      point.range = l.range;
      point.sphereRadius = l.shadows == LightShadows.Soft ? l.shadowRadius : 0.0f;
      point.falloff = FalloffType.Legacy;
    }

    public static void Extract(Light l, ref SpotLight spot)
    {
      spot.instanceID = l.GetInstanceID();
      spot.mode = LightmapperUtils.Extract(l.lightmapBakeType);
      spot.shadow = l.shadows != 0;
      spot.position = l.transform.position;
      spot.orientation = l.transform.rotation;
      Color colorTemperature = LightmapperUtils.ExtractColorTemperature(l);
      LinearColor lightColor = LinearColor.Convert(l.color, l.intensity);
      LinearColor indirect = LightmapperUtils.ExtractIndirect(l);
      LightmapperUtils.ApplyColorTemperature(colorTemperature, ref lightColor);
      LightmapperUtils.ApplyColorTemperature(colorTemperature, ref indirect);
      spot.color = lightColor;
      spot.indirectColor = indirect;
      spot.range = l.range;
      spot.sphereRadius = l.shadows == LightShadows.Soft ? l.shadowRadius : 0.0f;
      spot.coneAngle = l.spotAngle * ((float) Math.PI / 180f);
      spot.innerConeAngle = LightmapperUtils.ExtractInnerCone(l);
      spot.falloff = FalloffType.Legacy;
      spot.angularFalloff = AngularFalloffType.LUT;
    }

    public static void Extract(Light l, ref RectangleLight rect)
    {
      rect.instanceID = l.GetInstanceID();
      rect.mode = LightmapperUtils.Extract(l.lightmapBakeType);
      rect.shadow = l.shadows != 0;
      rect.position = l.transform.position;
      rect.orientation = l.transform.rotation;
      Color colorTemperature = LightmapperUtils.ExtractColorTemperature(l);
      LinearColor lightColor = LinearColor.Convert(l.color, l.intensity);
      LinearColor indirect = LightmapperUtils.ExtractIndirect(l);
      LightmapperUtils.ApplyColorTemperature(colorTemperature, ref lightColor);
      LightmapperUtils.ApplyColorTemperature(colorTemperature, ref indirect);
      rect.color = lightColor;
      rect.indirectColor = indirect;
      rect.range = l.range;
      rect.width = l.areaSize.x;
      rect.height = l.areaSize.y;
      rect.falloff = FalloffType.Legacy;
    }

    public static void Extract(Light l, ref DiscLight disc)
    {
      disc.instanceID = l.GetInstanceID();
      disc.mode = LightmapperUtils.Extract(l.lightmapBakeType);
      disc.shadow = l.shadows != 0;
      disc.position = l.transform.position;
      disc.orientation = l.transform.rotation;
      Color colorTemperature = LightmapperUtils.ExtractColorTemperature(l);
      LinearColor lightColor = LinearColor.Convert(l.color, l.intensity);
      LinearColor indirect = LightmapperUtils.ExtractIndirect(l);
      LightmapperUtils.ApplyColorTemperature(colorTemperature, ref lightColor);
      LightmapperUtils.ApplyColorTemperature(colorTemperature, ref indirect);
      disc.color = lightColor;
      disc.indirectColor = indirect;
      disc.range = l.range;
      disc.radius = l.areaSize.x;
      disc.falloff = FalloffType.Legacy;
    }

    public static void Extract(Light l, out Cookie cookie)
    {
      cookie.instanceID = (bool) (UnityEngine.Object) l.cookie ? l.cookie.GetInstanceID() : 0;
      cookie.scale = 1f;
      cookie.sizes = l.type != UnityEngine.LightType.Directional || !(bool) (UnityEngine.Object) l.cookie ? new Vector2(1f, 1f) : new Vector2(l.cookieSize, l.cookieSize);
    }
  }
}
